﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace WooCoo.Tools
{
    /// <summary>
    /// 引用类型判等
    /// </summary>
    public class ReferenceEqualityComparer : IEqualityComparer
    {
        protected ReferenceEqualityComparer()
        {
        }
        bool IEqualityComparer.Equals(object x, object y)
        {
            return object.ReferenceEquals(x, y);
        }
        int IEqualityComparer.GetHashCode(object obj)
        {
            if (obj == null)
            {
                return 0;
            }
            return obj.GetHashCode();
        }
    }

    /// <summary>
    /// 引用类型判等泛型支持
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public sealed class ReferenceEqualityComparer<T> : ReferenceEqualityComparer, IEqualityComparer<T>
    {
        private static ReferenceEqualityComparer<T> instance;
        public static ReferenceEqualityComparer<T> Instance
        {
            get
            {
                if (ReferenceEqualityComparer<T>.instance == null)
                {
                    ReferenceEqualityComparer<T> value = new ReferenceEqualityComparer<T>();
                    Interlocked.CompareExchange<ReferenceEqualityComparer<T>>(ref ReferenceEqualityComparer<T>.instance, value, null);
                }
                return ReferenceEqualityComparer<T>.instance;
            }
        }
        private ReferenceEqualityComparer()
        {
        }
        public bool Equals(T x, T y)
        {
            return object.ReferenceEquals(x, y);
        }
        public int GetHashCode(T obj)
        {
            if (obj == null)
            {
                return 0;
            }
            return obj.GetHashCode();
        }
    }
}
